﻿/*********************************************************************************************************************
Unit tests VanguardDocumentation, XML Schema SessionVariables

© 2014 Erik Ackerholm

This file is part of SqlVanguard.

SqlVanguard is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later 
version.

SqlVanguard is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with SqlVanguard. If not, see 
<http://www.gnu.org/licenses/>.
*********************************************************************************************************************/

set nocount on;
set xact_abort off;
GO

create table #TestCases (
	TestNo              integer not null           identity (1, 1),
	XmlDocument         nvarchar(max) not null,
	IsValid             bit not null,
	primary key (TestNo)
);
GO

insert into #TestCases
values
	('<Variables/>', 1),
	('<Variables>    </Variables>', 1),
	('', 0),
	('<SessionVariables/>', 0),
	('<Variables><BaseEntity/></Variables>', 0),
	('<Variables><BaseEntity Name=""/></Variables>', 1),
	('<Variables><BaseEntity Name="TestSchema"/></Variables>', 1),
	('<Variables><BaseEntity Type="SCHEMA"/></Variables>', 0),
	('<Variables><BaseEntity Type="TABLE" Name="TestSchema"/></Variables>', 1);
GO

declare @TestNo as integer;
declare @XmlDocument as nvarchar(max);
declare @IsValid as bit;

declare @TestVariable as xml(document VanguardDocumentation.SessionVariables);

set @TestNo = (select min(TestNo) from #TestCases);

while
	@TestNo is not null
begin
	
	-- Fetch test data
	set @XmlDocument = (select XmlDocument from #TestCases where TestNo = @TestNo);
	
	-- Reset output variables
	set @IsValid = null;
	
	begin try;
		set @TestVariable = @XmlDocument;
		set @IsValid = 1;
	end try
	begin catch
		set @IsValid = 0;
	end catch;
	
	if
		(select IsValid from #TestCases where TestNo = @TestNo) <> @IsValid
	begin
		print 'SessionVariables, Failed test no ' + rtrim(@TestNo);
		print '	' + coalesce(@XmlDocument, 'null')
		print case @IsValid
			when 0 then '	didn''t pass schema validation but should.'
			when 1 then '	passed schema validation but shouldn''t.'
		end;
	end;
	
	set @TestNo = (select min(TestNo) from #TestCases where TestNo > @TestNo);

end;
GO

drop table #TestCases;
GO

set nocount off;
GO




